package com.example.imitation_webchat_demo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.imitation_webchat_demo.R;
import com.example.imitation_webchat_demo.entity.MineItem;

//我的页面中使用的listview对应的适配器
public class MineAdapter extends BaseAdapter {
	private Context context;
	private List<MineItem> list;

	public MineAdapter(Context context, List<MineItem> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		MineItem item = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.mine_item, null);
			ImageView logoView = (ImageView) convertView.findViewById(R.id.mine_item_logo);
			TextView nameView = (TextView) convertView.findViewById(R.id.mine_item_text);
			viewHolder = new ViewHolder(logoView, nameView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.logoView.setImageResource(item.getResId());
		viewHolder.nameView.setText(item.getName());
		return convertView;
	}

	private class ViewHolder {
		ImageView logoView;
		TextView nameView;

		public ViewHolder(ImageView logoView, TextView nameView) {
			this.logoView = logoView;
			this.nameView = nameView;
		}
	}
}